#include <vpe_device.hh>
#include <cJSON.h>

int vpe_device::get_card_status(VpeCardInfo* card_array,int max_card_array_size,int* card_nums){

    int ret = Seirios_vpe_card_info( card_array, max_card_array_size, card_nums);
    return 0;
}

// void vpe_device::get_card_status_tojson(){
//     VpeCardInfo card_array[16] = {0};
//     int card_number = 0;
//     cJSON* cjson_card = NULL;
//     cJSON* cjson_cards = NULL;
//     cjson_cards = cJSON_CreateObject();
    

//     Seirios_vpe_card_info(card_array,16,&card_number);

//     for(int i=0; i<card_number; i++) {
//         char card_name[32] = {0};
//         cjson_card = cJSON_CreateObject();
//         cJSON_AddNumberToObject(cjson_card, "total_memory", card_array[i].total_mem);
//         cJSON_AddNumberToObject(cjson_card, "free_memory", card_array[i].free_mem);

//         cJSON_AddNumberToObject(cjson_card, "dec_free_resource", 100-card_array[i].dec_load);
//         cJSON_AddNumberToObject(cjson_card, "enc_free_resource", 100-card_array[i].enc_load);

//         cJSON_AddNumberToObject(cjson_card,"dec_total_resource_artificialhuman",card_array[i].total_dec_resource_artificialhuman);
//         cJSON_AddNumberToObject(cjson_card,"enc_total_resource_artificialhuman",card_array[i].total_enc_resource_artificialhuman);

//         cJSON_AddNumberToObject(cjson_card,"dec_free_resource_artificialhuman",card_array[i].total_dec_resource_artificialhuman-card_array[i].dec_free_resource_artificialhuman);
//         cJSON_AddNumberToObject(cjson_card,"enc_free_resource_artificialhuman",card_array[i].total_enc_resource_artificialhuman-card_array[i].enc_free_resource_artificialhuman);

//         sprintf(card_name,"transcoder%d",i);
//         cJSON_AddItemToObject(cjson_cards, card_name, cjson_card);
//     }
//     char*  str = cJSON_Print(cjson_cards);
//     printf("%s\n",str);
//     cJSON_Delete(cjson_cards);
// }

cJSON* vpe_device::to_json() {
    VpeCardInfo card_array[64] = {0};
    int card_number = 0;
    cJSON* cjson_cards = cJSON_CreateObject();
    Seirios_vpe_card_info(card_array,16,&card_number);

    for(int i=0; i<card_number; i++) {
        char card_name[64] = {0};
        cJSON* cjson_card = cJSON_CreateObject();
        cJSON_AddNumberToObject(cjson_card, "total_memory", card_array[i].total_mem);
        cJSON_AddNumberToObject(cjson_card, "free_memory", card_array[i].free_mem);

        cJSON_AddNumberToObject(cjson_card, "dec_free_resource", 100-card_array[i].dec_load);
        cJSON_AddNumberToObject(cjson_card, "enc_free_resource", 100-card_array[i].enc_load);

        cJSON_AddNumberToObject(cjson_card,"dec_total_resource_artificialhuman",card_array[i].total_dec_resource_artificialhuman);
        cJSON_AddNumberToObject(cjson_card,"enc_total_resource_artificialhuman",card_array[i].total_enc_resource_artificialhuman);

        cJSON_AddNumberToObject(cjson_card,"dec_free_resource_artificialhuman",card_array[i].total_dec_resource_artificialhuman-card_array[i].dec_free_resource_artificialhuman);
        cJSON_AddNumberToObject(cjson_card,"enc_free_resource_artificialhuman",card_array[i].total_enc_resource_artificialhuman-card_array[i].enc_free_resource_artificialhuman);

        sprintf(card_name,"transcoder%d",i);
        cJSON_AddItemToObject(cjson_cards, card_name, cjson_card);
    }

     return cjson_cards;
}

const char*  vpe_device::get_name()
{
    return "vpe_device";
}
vpe_device::~vpe_device(){

}
